﻿using Comm100.Framework.Domain.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using Bot.Domain.Bo;
using Castle.Windsor;
using Bot.Domain.Specifications;

namespace Bot.Domain.Categories.Service
{
    public class DetectIntentRecordDomainService : IDetectIntentRecordDomainService
    {
        private IWindsorContainer _container;
        private readonly IRepository<Guid, DetectIntentRecord> _repository;

        public DetectIntentRecordDomainService(IRepository<Guid, DetectIntentRecord> repository, IWindsorContainer container)
        {
            this._repository = repository;
            this._container = container;
        }

        public DetectIntentRecord Create(DetectIntentRecord category)
        {
            return _repository.Create(category);
        }

        public DetectIntentRecord Get(Guid id)
        {
            return _repository.Get(id);
        }

        public DetectIntentRecord Update(DetectIntentRecordUpdateBo bo)
        {
            DetectIntentRecord detectIntentRecord = _repository.Get(bo.Id);
            //detectIntentRecord.Id = bo.Id;
            //detectIntentRecord.SiteId = bo.SiteId;
            detectIntentRecord.BotId = bo.BotId;
            detectIntentRecord.UserQuestion = bo.UserQuestion;
            detectIntentRecord.Channel = bo.Channel;
            detectIntentRecord.SessionId = bo.SessionId;
            detectIntentRecord.IntentId = bo.IntentId;
            detectIntentRecord.Score = bo.Score;
            detectIntentRecord.AccountId = bo.AccountId;
            detectIntentRecord.AnswerType = bo.AnswerType;
            detectIntentRecord.RateType = bo.RateType;
            detectIntentRecord.SourceType = bo.SourceType;
            detectIntentRecord.CreatedTime = bo.CreatedTime;

            return detectIntentRecord;

        }

        public IEnumerable<ChatbotUsage> GetChatbotUsages(DateTime startDate, DateTime endDate, string include)
        {
            DetectIntentRecordFilterSpecification spec = new DetectIntentRecordFilterSpecification(startDate, endDate);
            IEnumerable<DetectIntentRecord> list = _repository.List(spec);
            IRepository<Guid, Chatbot> _botRepository = _container.Resolve<IRepository<Guid, Chatbot>>();
            var result = list.GroupBy(a => a.BotId).Select(t => new ChatbotUsage()
            {
                BotId = t.Key,
                UsedMessageAmount = t.Count(),
                Bot = _botRepository.Get(t.Key)
            });
            decimal thirdPartyBot = 1.0m;
            decimal ownBot = 1.0m;
            decimal.TryParse(ConfigOptionHelper.GetConfigOptionValueByKey("Bot_ThirdPartBotDiscount"), out thirdPartyBot);
            decimal.TryParse(ConfigOptionHelper.GetConfigOptionValueByKey("Bot_OwnBotDiscount"), out ownBot);
            foreach (var item in result)
            {
                if (item.Bot.EngineType == Enum.EnumBotType.thirdPartyBot)
                {
                    item.UsedMessageAmount = item.UsedMessageAmount * thirdPartyBot;
                }
                if (item.Bot.EngineType == Enum.EnumBotType.comm100OwnBot)
                {
                    item.UsedMessageAmount = item.UsedMessageAmount * ownBot;
                }
                if (string.IsNullOrEmpty(include) || !include.Equals("bot", StringComparison.OrdinalIgnoreCase))
                {
                    item.Bot = null;
                }
            }
            return result;
        }

        public void Delete(Guid id)
        {
            DetectIntentRecord detectIntentRecord = _repository.Get(id);

            _repository.Delete(detectIntentRecord);

        }
    }
}
